﻿#include "game.h"
#pragma warning(disable:6031)


//游戏进程
void game(void) {
	system("cls");
	char ret=0;
	char board[ROW][COL];
	InitBoard(board, ROW, COL); //棋盘初始化
	DisplayBoard(board, ROW, COL);//打印棋盘
	while(1){//循环进行游戏
		Player(board, ROW, COL);//玩家回合
		DisplayBoard(board, ROW, COL);//打印棋盘
		ret = iswin(board, ROW, COL);//判断输赢
		if (ret != 'c')
			break;
		Computer(board, ROW, COL);//电脑回合
		DisplayBoard(board, ROW, COL);//打印棋盘
		ret = iswin(board, ROW, COL);//判断输赢
		if (ret != 'c')
			break;
	}
	if (ret == '*')
		printf("恭喜你获胜啦！再来一局肯定更加精彩！\n\n");
	else if (ret == '#')
		printf("再接再励，偶尔的失误不算什么~\n\n");
	else if(ret == 'Q')
		printf("势均力敌的对手，这一战酣畅淋漓！\n\n");
}
//游戏菜单
void menu(void) {
	printf("**********************\n");
	printf("******  1.Play  ******\n");
	printf("******  0.Exit  ******\n");
	printf("**********************\n");
}
//棋盘初始化
void InitBoard(char board[ROW][COL], int rol, int col) {
	int i, j;
	for (i = 0;i < ROW;i++) {
		for (j = 0;j < COL;j++) {
			board[i][j] = ' ';
		}
	}
}
//打印棋盘
void DisplayBoard(char board[ROW][COL], int rol, int col) {
	int i = 0;
	int j = 0;
	for (i = 0; i < rol; i++) {
		for (j = 0;j < col ;j++) {
			printf(" %c ", board[i][j]);
			if(j<col-1)
			printf("|");
		}
		printf("\n");
		for (j = 0;j < col;j++) {
			if (i < rol - 1)
				printf("---");
			if (j < col - 1 && i<rol-1)
				printf("|");
		}
		printf("\n");


	}
}
//玩家回合
void Player(char board[ROW][COL], int row, int col) {
	printf("输入坐标开始下棋，行不超过%d,列不超过%d，中间用空格隔开\n", ROW, COL);
	int x = 0, y = 0;
	while (1) {
		printf("请输入：");
		scanf("%d %d", &x, &y);
		if (0 < x && x <= row && 0 < y && y <= col && board[y - 1][x - 1] == ' ')
		{
			board[y - 1][x - 1] = '*';
			break;
		}
		else if (0 < x && x <= row && 0 < y && y <= col && board[y - 1][x - 1] != ' ')
			printf("该位置已被占用，换一个位置吧\n");
		else
			printf("坐标范围超了，请输入正确的坐标\n");
	}
}

//电脑回合
void Computer(char board[ROW][COL], int row, int col) {
	system("cls");

	int i = 0, j = 0, countp = 0, countc = 0,k=-1;
	//遍历胜利可能
	//遍历列
	for (i = 0;i < row; i++) {
		countc = 0;
		k = -1;
		for (j = 0;j < col;j++) {
			if (board[i][j] == '#')
				countc++;
			else if (board[i][j] == ' ')
				k = j;
		}
		if (countc == 2 && k != -1) {
			board[i][k] = '#';
			return;
		}

	}
	//遍历行
	for (j = 0;j < col; j++) {
		countc = 0;
		k = -1;
		for (i = 0;i< row;i++) {
			if (board[i][j] == '#')
				countc++;
			else if (board[i][j] == '" "' )
				k = i;
		}
		if (countc == 2 && k != -1) {
			board[k][j] = '#';
			return;
		}

	}
	//遍历 / 对角线
	int count = 0;
	k = -1;
	for (i = 0;i < row;i++)
	{
		if (board[i][row - i - 1] == '#')
			count++;
		else if (board[i][row - i - 1] == ' ')
			k = i;
		if (2 == count && k != -1)
		{
			board[i][row - i - 1] = '#';
			return;
		}
	}
	//遍历 \ 对角线
	count = 0;
	k = -1;
	for (i = 0;i < row;i++)
	{
		if (board[i][i] == '#')
			count++;
		else if (board[i][i] == ' ')
			k = i;
		if (2 == count && k != -1)
		{
			board[i][i] = '#';
			return;
		}
	}

	//遍历堵截
	//遍历列
	for (i = 0;i < row; i++) {
		countc = 0;
		k = -1;
		for (j = 0;j < col;j++) {
			if (board[i][j] == '*')
				countc++;
			else if (board[i][j] == ' ')
				k = j;
		}
		if (countc == 2 && k != -1) {
			board[i][k] = '#';
			return;
		}

	}
	//遍历行
	for (j = 0;j < col; j++) {
		countc = 0;
		k = -1;
		for (i = 0;i < row;i++) {
			if (board[i][j] == '*')
				countc++;
			else if (board[i][j] == ' ')
				k = i;
		}
		if (countc == 2 && k != -1) {
			board[k][j] = '#';
			return;
		}

	}
	//遍历 / 对角线
	count = 0;
	k = -1;
	for (i = 0;i < row;i++)
	{
		if (board[i][row - i - 1] == '*')
			count++;
		else if (board[i][row - i - 1] == ' ')
			k = i;
		if (2 == count && k != -1)
		{
			board[k][row - k - 1] = '#';
			return;
		}
	}
	//遍历 \ 对角线
	count = 0;
	k = -1;
	for (i = 0;i < row;i++)
	{
		if (board[i][i] == '*')
			count++;
		else if (board[i][i] == ' ')
			k = i;
		if (2 == count && k != -1)
		{
			board[k][k] = '#';
			return;
		}
	}
	if (board[row / 2][col / 2] == ' ')
		board[row / 2][col / 2] = '#';
	else {
		int x = 0;
		int y = 0;
		while (1) {
			x = rand() % row;
			y = rand() % col;
			if (board[x][y] == ' ') {
				board[x][y] = '#';
				break;
			}
		}
	}
}

//判断输赢
char iswin(char board[ROW][COL], int row, int col) {
	int i = 0, j = 0;
	//判断列
	for (i = 0;i < row;i++)
	{
		j = 1;
		for (j = 1;j < col;j++)
		{
			if (board[i][0] != board[i][j] || board[i][0]==' ')
				break;
		}
		if (col == j)
			return board[i][0];
	}
	//判断行
	for (i = 0;i < col;i++)
	{
		j = 1;
		for (j = 1;j < row;j++)
		{
			if (board[0][i] != board[j][i] || board[0][i] == ' ')
				break;
		}
		if (row == j)
			return board[0][i];
	}
	//判断对角线
	for (i = 1;i < row;i++) {
		if (board[0][0] != board[i][i] || board[0][0] == ' ')
			break;
		if (i == row - 1)
			return board[0][0];
	}
	for (i = 1;i < row;i++) {
		if (board[0][row - 1] != board[i][row - 1 - i] || board[0][row-1] == ' ')
			break;
		if (i == row - 1)
			return board[0][row - 1];
	}
	//判断平局与继续
	for (i = 0;i < row;i++) {
		j = 0;
		for (j = 0;j < col;j++)
		{
			if (board[i][j] == ' ')
				return 'c';
		}
	}
	return 'Q';
}